package com.campusevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campusevaluation.common.exception.BasePage;
import com.campusevaluation.common.exception.Result;
import com.campusevaluation.domain.dto.DepartmentDTO;
import com.campusevaluation.domain.po.Department;
import com.campusevaluation.dao.mapper.DepartmentMapper;
import com.campusevaluation.domain.vo.DepartmentVO;
import com.campusevaluation.service.IDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
implements IDepartmentService {
    @Override
    public Result<String> createDepartment(DepartmentDTO departmentDTO) {
        if (departmentDTO.getName() == null || departmentDTO.getName().trim().isEmpty()) {
            return Result.error("部门名称不能为空");
        }
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", departmentDTO.getName());
        Department department = getOne(queryWrapper);
        if (department != null) {
            return Result.error("该部门已存在");
        }
        department = new Department();
        if (departmentDTO.getParentName() != null && !departmentDTO.getParentName().trim().isEmpty()) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", departmentDTO.getParentName());
            Department parent = getOne(queryWrapper);
            if (parent == null) {
                return Result.error("该上级部门不存在");
            }
            department.setParent_id(parent.getId());
        }
        department.setName(departmentDTO.getName());
        save(department);
        return Result.success(null, "部门创建成功");
    }

    @Transactional
    @Override
    public Result<String> deleteDepartment(Long id) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        List<Department> list = getBaseMapper().selectList(queryWrapper);
        if (!list.isEmpty()) {
            for (Department department : list) {
                department.setParent_id(null);
                updateById(department);
            }
        }
        boolean remove = this.removeById(id);
        if (!remove) {
            return Result.error("部门删除失败");
        }
        return Result.success(null, "部门删除成功");
    }

    @Override
    public Result<Page<Department>> getDepartmentsByPage(BasePage basePage) {
        Page<Department> pageInfo = new Page<>(basePage.getPageNum(), basePage.getPageSize());
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Department::getId);
        Page<Department> page = this.page(pageInfo, queryWrapper);
        if (page.getTotal() == 0) {
            return Result.error("暂无部门信息");
        }
        return Result.success("查询成功", pageInfo);
    }

    @Override
    public Result<DepartmentVO> getDepartmentById(Long id) {
        Department department = this.getById(id);
        if (department == null) {
            return Result.error("获取部门信息失败");
        }
        DepartmentVO departmentVO = new DepartmentVO();
        BeanUtils.copyProperties(department, departmentVO);
        if (department.getParent_id() != null) {
            departmentVO.setParent(this.getById(department.getParent_id()).getName());
        }
        return Result.success("获取部门信息成功", departmentVO);
    }

    @Transactional
    @Override
    public Result<String> updateDepartment(DepartmentDTO departmentDTO) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentDTO, department);
        Department parent = this.getById(department.getParent_id());
        department.setParent_id(parent == null ? null : parent.getId());
        boolean update = this.updateById(department);
        if (!update) {
            return Result.error("更新部门信息失败");
        }
        return Result.success(null, "部门信息更新成功");
    }

    @Transactional
    @Override
    public Result<String> deleteBatchByIds(List<Long> ids) {
        for (Long id : ids) {
            Department department = this.getById(id);
            if (department == null) {
                return Result.error("删除的部门不存在");
            }
            List<Department> list = query().lambda().eq(Department::getParent_id, id).list();
            for (Department department1 : list) {
                department1.setParent_id(null);
            }
        }
        boolean remove = this.removeBatchByIds(ids);
        if (!remove) {
            return Result.error("删除部门失败");
        }
        return Result.success(null, "删除部门成功");
    }
}
